package org.skipper.samples.httpclient5;

import com.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
import org.apache.hc.client5.http.async.methods.SimpleHttpResponse;
import org.apache.hc.client5.http.async.methods.SimpleRequestBuilder;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.async.CloseableHttpAsyncClient;
import org.apache.hc.client5.http.impl.auth.BasicCredentialsProvider;
import org.apache.hc.client5.http.protocol.HttpClientContext;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.HttpStatus;
import org.apache.hc.core5.util.Timeout;
import org.omg.CORBA.TIMEOUT;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.net.URI;
import java.net.URLEncoder;
import java.util.concurrent.*;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author shoujian
 * Date: 2022-06-22
 * Time: 16:38
 */
@Component
public class Test {


    private static ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
            .setNameFormat("show-record-pool-%d").build();

    private static ExecutorService service = new ThreadPoolExecutor(50, 200,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(1024), namedThreadFactory,
            new ThreadPoolExecutor.AbortPolicy());

    @Resource
    @Qualifier("httpClientBuilder")
    private CloseableHttpAsyncClient httpAsyncClient;

    @PostConstruct
    public void test() throws Exception {

        httpAsyncClient.start();

        JSONObject msg = new JSONObject();
        msg.put("MsgTag", 10034000);
        msg.put("actorIncome", 1223423);

        final SimpleHttpRequest options = SimpleRequestBuilder.get(URI.create("http://nodeapi.kktv2.com:8080/"))
                .addParameter("type", "2")
//                .addParameter("userId","160002847")
                .addParameter("roomId", "10000993")
                .addParameter("msg", msg.toString())
                .build();

        final HttpClientContext context = HttpClientContext.create();
        context.setCredentialsProvider(new BasicCredentialsProvider());

        final Future<SimpleHttpResponse> future = httpAsyncClient.execute(options, context, null);

        try {
            future.get(Timeout.ofSeconds(5).getDuration(), Timeout.ofSeconds(5).getTimeUnit());
        } catch (final ExecutionException ex) {
            final Throwable cause = ex.getCause();
        } catch (final TimeoutException ex) {
        }


    }

    static class WorkerThread implements Runnable {

        private final String type;

        private final String roomId;

        private final String msg;

        public WorkerThread(
                final String type,
                final String roomId,
                final String msg
        ) {
            this.type = type;
            this.roomId = roomId;
            this.msg = msg;
        }

        @Override
        public void run() {

        }
    }


}
